/**
 * Created with IntelliJ IDEA.
 * User: Hkkkk Guo
 * Date: 2023/2/10
 * Time: 12:19
 * Description:
 */
package com.hangkai.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hangkai.reggie.dao.CategoryDao;
import com.hangkai.reggie.domain.Category;
import com.hangkai.reggie.domain.Dish;
import com.hangkai.reggie.domain.Setmeal;
import com.hangkai.reggie.exception.CustomException;
import com.hangkai.reggie.service.CategoryService;
import com.hangkai.reggie.service.DishService;
import com.hangkai.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类 删除前进行判断
     * @param id
     */
    @Override
    public void remove(Long id){
        //菜品里找关联
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId, id);
        int count = dishService.count(lqw);
        //套餐里找关联
        LambdaQueryWrapper<Setmeal> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count(lqw2);

        if(count > 0){//查询当前分类是否关联了菜品 如果是 则抛出业务异常
            throw new CustomException("当前类别关联了菜品 不可删除");
        }
        if(count2 > 0){//查询当前分类是否关联了套餐 如果是 则抛出业务异常
            throw new CustomException("当前类别关联了套餐 不可删除");
        }
        //正常删除分类
        super.removeById(id);
    }

}
